var currentPrice=1;
var remainTime=1;
var startLeftTime=0;
var queryInterval=10000;//10s 查询一次当前价格
var countDown=0;
var timeId=0;
var priceLimit = parseInt(/\d+/.exec($(".cost del").html())*1*0.5);
var addr = document.location.href;
var uid = /[\d]{4,8}/.exec(addr)[0];
var code = "<div id='qp_div' style='background:yellow'>"
		+ "剩余几秒开始<input type='text' id='qp_startLeftTime' min='5' value='10'/>"
		+ "最高出价<input type='text' id='qp_maxPrice' min='5' value='10'/>"
		+ "加价幅度<input type='text' id='qp_addPrice' min='1' value='2'/>"
		+ "<p id='qp_tip'>V0.5</p>"
		+ "<p id='qp_bid'>price</p>"
		+ "<input type='button' value='准备开抢' id='qp_btn_begin' class='qp_btn'/>"
		+ "<input type='button' value='刷新价格' id='qp_btn_refresh' class='qp_btn'/>"
		+ "</div>";
$('.intro_detail').append(code);
$('#qp_maxPrice').val(priceLimit);
$('#qp_btn_begin').on('click', function(){beginPaimai()});
$('#qp_btn_refresh').on('click', function(){queryPrice()});

function PrintLog(msg){
	$("#qp_tip").html("<p id='qp_tip'>"+msg+"</p>");
}
function PrintBidLog(msg){
	$("#qp_bid").html("<p id='qp_bid'>"+msg+"</p>");
}
function getRamdomNumber(){
	var num=""; 
	for(var i=0;i<6;i++) 
	{ 
		num+=Math.floor(Math.random()*10); 
	} 
	return num;
}
function beginPaimai()
{
	startLeftTime=$("#qp_startLeftTime").val()*1000;
	queryPrice();
	setInterval(remainTimeCountDown,100);
}
function remainTimeCountDown(){
	if(countDown==1 && remainTime>0) {
		remainTime = remainTime - 100;
		PrintLog(remainTime/1000+"秒后开始抢拍");
		// if (remainTime*1 < 1500) {
				// var price=$('#qp_maxPrice').val();
				// if(currentPrice*1<price*1)
				// {
					// clearInterval(timeId);
					// countDown=0;
					// console.log("bid");
					// PrintLog("出价");
					// bid();
				// }
				// else{
					// clearInterval(timeId);
					// countDown=0;
					// console.log("currentPrice="+currentPrice+"   maxPrice="+price+"");
					// PrintLog("当前价已超过最高出价");
				// }
		// }
		// else 
		if (parseInt(remainTime)<10000) {
			if(queryInterval!=200)
			{
				queryInterval=200;
				timeId=setInterval(queryPrice,queryInterval);
				console.log("queryInterval="+200);
			}
		}
		else
		{
			if(queryInterval!=5000)
			{
				queryInterval=5000;
				timeId=setInterval(queryPrice,queryInterval);
				console.log("queryInterval="+5000);
			}
		}
	}
}
function queryPrice(){
	if(remainTime>0) {
		var paimaiId=$("#paimaiId").val();
		var maxprice=$('#qp_maxPrice').val();
		var url = "/json/current/englishquery?paimaiId="+paimaiId+"&skuId=0&t="+getRamdomNumber()+"&start="+0+"&end="+1;
		$.getJSON(url,function (response) {
			remainTime=response.remainTime;
			currentPrice=response.currentPrice;
			var currentPriceInfo = "<em class=\"font12\">&yen;</em>"+currentPrice;
			$("#auction3dangqianjia").html(currentPriceInfo);
			var add_price=$("#qp_addPrice").val();
			$("#bidPrice").val(currentPrice*1+add_price*1);
			console.log("currentPrice="+currentPrice+"   remainTime="+remainTime);
			PrintLog("更新成功，当前价:"+currentPrice);
			if(parseInt(currentPrice)>parseInt(maxprice))
			{
				countDown=0;
				PrintLog("当前价已超过最高出价");
			}
			else{
				countDown=1;
				if(remainTime*1 < 2000)
				{
					countDown=0;
					console.log("bid");
					//clearInterval(timeId);
					bid();
				}
			}
		});	
	}
}
function bid(){//出价
	var paimaiId=$("#paimaiId").val();
	var add_price=$("#qp_addPrice").val();
	var price =currentPrice*1+add_price*1;
	if(true){
		var url = "/services/bid.action?t=" + getRamdomNumber();
	    var data = {paimaiId:paimaiId,price:price,proxyFlag:0,bidSource:0};
		console.log("bid_price="+price);
		PrintBidLog("bid_price="+price);
	    jQuery.getJSON(url,data,function(jqXHR){
			if(jqXHR!=undefined){
				if(jqXHR.result=='200'){
					console.log("恭喜您，出价成功");
					PrintLog("恭喜您，出价成功");
					//dialogSuccess("恭喜您，出价成功",5,"秒钟后窗口将会自动关闭");
				}else if(jqXHR.result=='login'){
					window.location.href='http://passport.jd.com/new/login.aspx?ReturnUrl='+window.location.href;
				}else if(jqXHR.result=='600'){
					console.log("很抱歉，出价失败!");
					PrintLog("很抱歉，出价失败!");
					//dialogError("很抱歉，出价失败",5,"当日已流拍两次，不能继续出价");
				}else if(jqXHR.result=='573'){
					console.log("很抱歉，出价失败!");
					PrintLog("很抱歉，出价失败!");
					//dialogError("啊哦，出价失败",5,"欢迎再次参与~");
				}else{
					console.log("很抱歉，出价失败!"+jqXHR.message);
					PrintLog("很抱歉，出价失败!"+jqXHR.message);
					//dialogError("很抱歉，出价失败",5,jqXHR.message);
				}
				//pageCurrentReload();
			}
	    });
	}
}







